package com.chenyue.cm.news.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.news.dao.JdNewsCommentMapper;
import com.chenyue.cm.news.dao.JdNewsMapper;
import com.chenyue.cm.news.dao.JdNewsViewRecordMapper;
import com.chenyue.cm.news.domain.JdNews;
import com.chenyue.cm.news.domain.JdNewsComment;
import com.chenyue.cm.news.domain.JdNewsViewRecord;
import com.chenyue.cm.news.service.JdNewsService;
import com.chenyue.cm.news.vo.IsCollect;
import com.chenyue.cm.news.vo.NewInfoVo;
import com.chenyue.cm.user.dao.JdUserCollectionMapper;
import com.chenyue.cm.user.domain.JdUserCollection;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * @author chenyue-cm
 */
@Service(value = "JdNewsService")
public class JdNewsServiceImpl extends BaseServiceImpl<JdNews> implements JdNewsService {

    private final JdNewsMapper jdNewsMapper;
    private final JdNewsViewRecordMapper jdNewsViewRecordMapper;
    private final JdNewsCommentMapper jdNewsCommentMapper;
    private final JdUserCollectionMapper jdUserCollectionMapper;

    public JdNewsServiceImpl(
            @Qualifier("jdNewsMapper") JdNewsMapper jdNewsMapper,
            @Qualifier("jdNewsViewRecordMapper") JdNewsViewRecordMapper jdNewsViewRecordMapper,
            @Qualifier("jdNewsCommentMapper") JdNewsCommentMapper jdNewsCommentMapper,
            @Qualifier("jdUserCollectionMapper") JdUserCollectionMapper jdUserCollectionMapper) {
        this.jdNewsMapper = jdNewsMapper;
        this.jdNewsViewRecordMapper = jdNewsViewRecordMapper;
        this.jdNewsCommentMapper = jdNewsCommentMapper;
        this.jdUserCollectionMapper = jdUserCollectionMapper;
    }

    @Override
    @Resource(name = "jdNewsMapper")
    public void setBaseMapper(LcBaseMapper<JdNews, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public NewInfoVo getNewsInfo(Integer newsId, Integer userId) {
        if (userId > 0) {
            JdNewsViewRecord newView = new JdNewsViewRecord();
            newView.setNewsId(newsId);
            newView.setUserId(userId);
            newView.setCt(DateUtils.getToday());
            jdNewsViewRecordMapper.insert(newView);
        }
        JdNews newsInfo = jdNewsMapper.selectById(newsId);
        NewInfoVo vo = new NewInfoVo();
        vo.setId(newsInfo.getId());
        vo.setTitle(newsInfo.getTitle());
        vo.setContent(newsInfo.getContent());
        vo.setCover(newsInfo.getCover());
        vo.setIsRecommend(newsInfo.getIsRecommend());
        vo.setCu(newsInfo.getCu());
        //间隔时间
        String convertDate = DateUtils.convertDate2Display(newsInfo.getCt());
        vo.setConvertDate(convertDate);
        //浏览次数
        LambdaQueryWrapper<JdNewsViewRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdNewsViewRecord::getNewsId, newsId);
        int views = jdNewsViewRecordMapper.selectCount(wrapper) + 1;
        vo.setViewNum(views);
        //收藏次数
        LambdaQueryWrapper<JdUserCollection> collectWrapper = new LambdaQueryWrapper<>();
        collectWrapper.eq(JdUserCollection::getColType, 2)
                .eq(JdUserCollection::getColId, newsId);
        Integer collects = jdUserCollectionMapper.selectCount(collectWrapper);
        vo.setCollectNum(collects);
        //是否收藏
        collectWrapper.eq(JdUserCollection::getUserId, userId);
        if (jdUserCollectionMapper.selectCount(collectWrapper) > 0) {
            vo.setIsCollect(1);
        } else {
            vo.setIsCollect(0);
        }
        return vo;
    }

    @Override
    public Page<List<NewInfoVo>> getAllNewsList(Integer page, Integer rows) {
        LambdaQueryWrapper<JdNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdNews::getIsShow, 1);
        wrapper.orderByDesc(JdNews::getIsRecommend);
        wrapper.orderByDesc(JdNews::getCt);
        Integer total = jdNewsMapper.selectCount(wrapper);
        Page<List<NewInfoVo>> result = new Page<>(page, rows, total);
        wrapper.last(result.convertPage());
        List<JdNews> jdNews = jdNewsMapper.selectList(wrapper);
        List<NewInfoVo> voList = toNewVo(jdNews);
        result.setContent(voList);
        return result;
    }

    @Override
    public List<NewInfoVo> getNewsListByUser(Integer userId) {
        LambdaQueryWrapper<JdNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdNews::getIsShow, 1).eq(JdNews::getCu, userId);
        wrapper.orderByDesc(JdNews::getIsRecommend);
        wrapper.orderByDesc(JdNews::getCt);
        List<JdNews> jdNews = jdNewsMapper.selectList(wrapper);
        return toNewVo(jdNews);
    }

    private List<NewInfoVo> toNewVo(List<JdNews> jdNews) {
        List<NewInfoVo> news = new ArrayList<>();
        for (JdNews jdNew : jdNews) {
            JdNews newsInfo = jdNewsMapper.selectById(jdNew.getId());
            NewInfoVo vo = new NewInfoVo();
            vo.setId(newsInfo.getId());
            vo.setTitle(newsInfo.getTitle());
            vo.setContent(newsInfo.getContent());
            vo.setCover(newsInfo.getCover());
            vo.setIsRecommend(newsInfo.getIsRecommend());
            vo.setCu(newsInfo.getCu());
            //间隔时间
            String convertDate = DateUtils.convertDate2Display(newsInfo.getCt());
            vo.setConvertDate(convertDate);
            //浏览次数
            LambdaQueryWrapper<JdNewsViewRecord> viewWrapper = new LambdaQueryWrapper<>();
            viewWrapper.eq(JdNewsViewRecord::getNewsId, jdNew.getId());
            Integer views = jdNewsViewRecordMapper.selectCount(viewWrapper);
            vo.setViewNum(views);
            //收藏次数
            LambdaQueryWrapper<JdUserCollection> collectWrapper = new LambdaQueryWrapper<>();
            collectWrapper.eq(JdUserCollection::getColType, 2)
                    .eq(JdUserCollection::getColId, jdNew.getId());
            Integer collects = jdUserCollectionMapper.selectCount(collectWrapper);
            vo.setCollectNum(collects);
            //评论条数
            LambdaQueryWrapper<JdNewsComment> commentWrapper = new LambdaQueryWrapper<>();
            commentWrapper.eq(JdNewsComment::getNewsId, jdNew.getId());
            Integer comments = jdNewsCommentMapper.selectCount(commentWrapper);
            vo.setCommentNum(comments);
            news.add(vo);
        }
        return news;
    }

    @Override
    public Success<IsCollect> collectNews(Integer newsId, Integer userId) {
        IsCollect collect = new IsCollect();
        LambdaQueryWrapper<JdUserCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JdUserCollection::getColType, 2)
                .eq(JdUserCollection::getColId, newsId)
                .eq(JdUserCollection::getUserId, userId)
                .last(" limit 1 ");
        JdUserCollection userCollection = jdUserCollectionMapper.selectOne(wrapper);
        if (null != userCollection) {
            int success = jdUserCollectionMapper.deleteById(userCollection);
            if (success > 0) {
                collect.setIsCollect(0);
            } else {
                collect.setIsCollect(1);
            }
            return Success.success(collect);
        } else {
            JdUserCollection collection = new JdUserCollection();
            collection.setColType(2);
            collection.setColId(newsId);
            collection.setUserId(userId);
            int success = jdUserCollectionMapper.insert(collection);
            if (success > 0) {
                collect.setIsCollect(1);
            } else {
                collect.setIsCollect(0);
            }
            return Success.success(collect);
        }
    }
}
